﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Service.Contract;
using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views.EventProxies
{
    public class RespPartEventHandler : BaseResponseHandler<RespPartEventData>
    {
        protected override void OnHandle(RemoteEventProxy remoteEventProxy, ServiceProxy.MainServiceRef.Response response, RespPartEventData responseData)
        {
            if (string.Equals(UIContext.Current.AppType, "DISPLAYAPP", StringComparison.CurrentCultureIgnoreCase)
                && !UIContext.Current.IsUnderControl)
            {
                // 展示端且不在控制模式下，不处理来自服务器的切换事件
                return;
            }

            if (remoteEventProxy.RenderView != null)
            {
                ScenePart scene = remoteEventProxy.RenderView.GetCurrentPart() as ScenePart;
                if(scene == null)
                {
                    Common.Logger.ErrorToTag("Handler", "收到组件：{0}的事件，但是处理失败，当前场景为空", responseData.PartName);
                    return;
                }
                UI.Part part = scene.GetChildPart(responseData.PartName, true);
                if (part != null)
                {
                    part.ReceivePartEvent(responseData.ClientID, responseData.EventName, responseData.EventData);
                    remoteEventProxy.RenderView.UpdatePartViewState(part);
                }
                else
                {
                    Common.Logger.ErrorToTag("Handler", "收到组件：{0}的事件，但是处理失败，未发现该组件", responseData.PartName);
                }
            }
        }
    }
}
